/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package App1.bookShop.controllers;

import APP1.GUI2.MainFrame_2;
import APP1.bookShop.book.Book;
import APP1.bookShop.book.EBook;
import APP1.bookShop.book.PaperBook;
import APP1.bookShop.main.BookShop;
import APP1.bookShop.order.Order;
import APP1.bookShop.order.OrderBody;
import APP1.bookShop.people.Publisher;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.ListModel;

/**
 *
 * @author Asus
 */
public class OrderController {

    private BookShop bookShop;

    public OrderController(BookShop bookShop) {
        this.bookShop = bookShop;
    }

    public DefaultListModel getOrdersListModel(boolean ToBeprocessed) {
        DefaultListModel lm = new DefaultListModel();

        if (ToBeprocessed) {
            for (Order order : bookShop.getOrders()) {
                if (order.isProcessed()) {
                    lm.addElement(order);
                }
            }
        } else {

            for (Order order : bookShop.getOrders()) {

                lm.addElement(order);

            }
        }
        if (lm.size() <= 0) {
            lm.addElement("No Orders");
        }
        return lm;
    }

    public void handleDetailOrder(int selected) {
        if (selected >= 0) {
            Order o = bookShop.getOrders().get(selected);
            //update GUI
            if (o != null) {
                MainFrame_2 GUI = bookShop.getGUI();
                GUI.getOrderDatePicker().setDate(o.getOrderDate());
                GUI.getDeliveryDatePicker().setDate(o.getAgreedDeliveryDate());
                GUI.getPriceTextField().setText(o.getTotalPrice() + "");
                GUI.getOrderPublisherComboBox1().setSelectedItem(o.getPublisher());

            }
        }
    }

    public DefaultListModel getBooksToBeOrdered(Publisher pub, ListModel alreadyInOrder) {

        DefaultListModel lm = new DefaultListModel();
        if (alreadyInOrder != null) {
          
          
            for (Book book : pub.getBook()) {
                boolean present=false;
                for (int i = 0; i < alreadyInOrder.getSize(); i++) {
                    if(alreadyInOrder.getElementAt(i).equals(book)){
                        present=true;
                        break;
                    }
                }
                if (!present) {
                    lm.addElement(book);
                }

            }
        } 
        
        if (lm.size() <= 0) {
            lm.addElement("NO Books to be added are available");
        }
        return lm;
    }

    private boolean checkInput(Date orderDate, Date deliveryDate, String price,ListModel model) {
        
        boolean firstCondition= model!=null && model.getSize()>0 && orderDate != null && deliveryDate != null && price != null && price.length() > 0;
        return firstCondition;
    }

    public String addOrder(Date orderDate, Date deliveryDate, String price, ListModel model, Publisher pub) {
        if (!checkInput(orderDate, deliveryDate, price,model)) {
            return "Invalid Input";
        } else {
            Order o = new Order(orderDate, deliveryDate, Integer.parseInt(price), pub);
            o.setProcessed(false);
            for (int i = 0; i < model.getSize(); i++) {
                OrderBody ob = (OrderBody)model.getElementAt(i);
                o.getOrderBody().add(ob);
                
            }
            bookShop.getOrders().add(o);
            bookShop.getGUI().getOrderList().setModel(getOrdersListModel(!bookShop.getGUI().getPendingOrdersCheckBox().isSelected()));
            return "Saved";
        }
    }

    public DefaultListModel getOrderContentListModel(Order get) {
        DefaultListModel lm = new DefaultListModel();

        for (OrderBody orderBody : get.getOrderBody()) {

            lm.addElement(orderBody);

        }

        if (lm.size() <= 0) {
            lm.addElement("No Content");
        }
        return lm;
    }

    public void deleteOrder(Order get) {
        bookShop.getOrders().remove(get);
    }
}
